package com.vgemv.jsutilitysdkdemo.module.Sensor;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Orientation.JsOrientationManager;

import static com.vgemv.jsutilitysdk.Orientation.JsOrientationManager.JsOrientationBroadcastSensorOrientationDidChange;

public class SensorActivity extends Activity {

    RelativeLayout mRl_root;
    BroadcastReceiver mBroadcastReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRl_root = new RelativeLayout(this);
        mRl_root.setId(View.generateViewId());
        setContentView(mRl_root);

//        setupSensor2();
    }

    public void setupSensor2() {
        JsOrientationManager.sharedInstance().startMonitoring();
        setupBroadcastReceiver();
    }

    public void setupBroadcastReceiver() {
            //设置回调
            mBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if (action.equals(JsOrientationBroadcastSensorOrientationDidChange)) {
                        Log.d("Orientation", "on receive action="+intent.getAction());
                        int newOrientation = intent.getIntExtra(JsOrientationManager.JsOrientationNew,0);
//                        adjustOrientation(newOrientation);
                    }
                }
            };
            //监听者注册广播
            IntentFilter filter = new IntentFilter();
            filter.addAction(JsOrientationBroadcastSensorOrientationDidChange);
            this.registerReceiver(mBroadcastReceiver, filter);
    }

    public void setupSensor() {
        SensorEventListener sensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                JsLogManager.logc_d("onSensorChanged","onSensorChanged");
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {

            }
        };
        SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        sm.registerListener(
                sensorEventListener,
                sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
